package graphics.screens;

import graphics.BootColor;

import java.io.IOException;
import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import tool.ImageUtils;
import tool.Path;
import boot.Boot;

public class DeleteReportScreen extends Canvas implements CommandListener {

	private final Command okCommand = new Command("OK", Command.OK, 1);

	public DeleteReportScreen() {
		setup();
	}

	public void setup() {
		addCommand(okCommand);
		setCommandListener(this);
	}

	protected void paint(Graphics graphics) {
		graphics.setColor(BootColor.BLACK);
		graphics.fillRect(0, 0, 240, 250);

		drawSplash(graphics);

		graphics.setColor(BootColor.FLASHY_MENU_COLOR);
		int number = getNumDeleted();
		String tuneWord = "tunes";
		if (number == 1) {
			tuneWord = "tune";
		}

		graphics.drawString(number + " " + tuneWord + " deleted.", 120,
				(graphics.getFont().getHeight()) * 3 / 2, Graphics.TOP
						| Graphics.HCENTER);
	}

	private void drawSplash(Graphics graphics) {
		Image splash = ImageUtils.getImageFromImageFolder("/Musique-mini.png");
		graphics.drawImage(splash, 0, 0, Graphics.LEFT | Graphics.TOP);
	}

	public void commandAction(Command command, Displayable disp) {

		if (command == okCommand) {
			Boot.getInstance().readyToGo();
		}
	}

	private int getNumDeleted() {
		int result = 0;
		String newDir = Path.NOW;
		try {
			FileConnection connection = (FileConnection) Connector.open(newDir);
			Enumeration files = connection.list("*.delete", true);
			while (files.hasMoreElements()) {
				files.nextElement();
				result++;
			}
			connection.close();
		} catch (IOException e) {
			Boot.alertException(e, this, "getNumDeleted()");
		}
		return result;
	}

}
